Create Navigation Menu In WordPress Theme

How To Create Navigation menu In WordPress Theme Within A Minute?

When you develop a child theme or any WordPress theme then you may in need to create navigation menu in WordPress theme. It’s one of the important parts of the website.

Are you thinking about adding the theme support option to show the navigation menu to your website? Well, for the navigation menu, you have to register it using a function. If you are a blogger then you just have to copy and paste the code.

Though most of the web developers try their best to create navigation menu in WordPress theme so that the users won’t face any problem.

But in case, if you want to add it yourself then you have to create a function to register the menu to WordPress.

What To Do To Create Navigation Menu In WordPress Theme?

If you know the WordPress theme file structure then you would know about the functions.php file. In this process, you have to use this file.

There are some steps to take.

Step 1:- First of all, you have to create a function to register the navigation menu.

function your_nav_menu(){

register_nav_menu(‘new menu’, __( ‘New Menu’ ));

}

add_action( ‘init’, ‘your_nav_menu’ );

In th above code “your_nav_menu” is the name of the function. You can replace the name of the function according to your theme. “New Menu” is the name appear in your WordPress “Menus” section for this navigation menu.

Step 2:- Now you have to place the navigation menu where you want to show it to your WordPress theme. If you are creating the header navigation menu then put it in the header.

If you want to add it in the footer then this can also be done.

<nav>

<?php wp_nav_menu( array ( ‘theme_location’ => ‘new menu’)); ?>

</nav>

You can place this code in any section of your WordPress theme. Just make sure that you use the same name which is registered. Only the PHP code would work for showing the menu. But you know that HTML is necessary to embed the PHP codes.

As you can see in the above function, you have registered the “new menu“. So you have to call the same name wherever you want to show the navigation menu.

If you change the name then nothing will happen. It is case sensitive, so make sure that you don’t use upper words instead of lower.

You Can Add More Than One Navigation Menu In WordPress.

create navigation menu in wordpress theme

You may have read that people used to ask if they can add more than one navigation menu in their theme. Well, the solution is here.

You just have to do the small change in the above-mentioned codes. While registering, you have to register all the navigation menus. The same while calling them.

Let me show you how.

Step 1:- Register all the navigation menus together in the same function.

function your_nav_menus(){register_nav_menus(array(

‘primary’ => __(‘Header Menu’),

‘footer’ => __(‘Footer Menu’),

));}
add_action( ‘init’ , ‘your_nav_menus’);

Here, two navigation menus are registered. you can add as many navigation menus you want. Just add an additonal line of the code for each menu.

Step 2:- Now you have to place the code to show these navigation menus.

<nav>

<?php

$args = array( ‘theme_location’ => ‘primary’);

?>

<?php wp_nav_menu( $args); ?>

</nav>

Using this code, you can call the first menu. The same code can be used for the second and all the menus you would create. Just replace the name of the menu with the name you registered.

As I have described in the single menu, you can also use that code. In this code, I have passed an argument for the better coding.

Are You Ready To Create Navigation Menu In WordPress Theme?

The above codes would do the work for you. It depends on the way a developer provides the code. I have mentioned the arguments. But there are many developers who prefer some other way.

The main point is to create navigation menu in WordPress theme. It can be fulfilled using these codes. If you want to show two menus together then just call the registered navigation menus.

In a WordPress theme, it’s very important to guide the readers about the topics to be covered. You can show many menus to your website. If you still face any problem then feel free to ask.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, design issues and what not.


Get Free Updates Into Your Inbox

Learn Everything Just Like I Did

SUBSCRIBE



6 comments

  1. Hi Ravi,

    Most of the blogger in WordPress don’t create a child theme first of all. Thought they create, one does not know much and keeps on adding plugins. And instantly land up with the slow site, which includes me. ?

    I always loved coding though I don’t have much idea about it. And adding and creating navigation menu in WordPress theme, I never thought it is that simple. And you made my day…. Thanks for the simple and nice tutorial…. Keep up bro…

    Take Care

    Ugyen

    1. Hey Ugyen,

      I agree with your point about the bloggers and the use of the plugins. But what’s the need to use any plugin if you can do the things just with the codes.

      Though it would be hard for the bloggers to code but still, adding a navigation menu in your theme is a way easier than you think.

      Thanks for stopping by.

      Have a great day.

      ~Ravi

  2. Hi Ravi,

    This is so awesome! You don’t know how many times I’ve been asked to create new nav menu’s and I’ve had to say “No”. The tutorial I was following seemed too complicated for me to dive into. However, now that I know YOU know how to do it, I’ll send them your way.

    Thanks for sharing this! I might give it a shot on one of my sites. ?

    Happy Weekend!

    B

    1. Hey Bren,

      When I started blogging then I searched for the tutorial to add more navigation menus in the WordPress theme. But nothing worked for me. Sometimes we have to learn and then implement.

      You can add as many navigation menus as you want. Just one function and register many menus.

      Hope this would help you.

      Thanks for the support.

      Enjoy the day.

      ~Ravi

  3. Hey Ravi,

    WordPress comes with a navigation menu system that makes it quite easy for beginner users to create and manage menus. The location of the menus can vary from theme to theme. Almost all WordPress themes come with at least one menu location. Some WordPress themes come with multiple menu locations to accommodate for more complex websites.You probably noticed that each menu item you added is arranged in the order you added them.When you add pages or categories to your custom navigation menu, WordPress automatically uses the page title or category name as the link text.

    As for social icons, some themes may come with pre-built social media icons in the menus. Alternatively, you can use a plugin like Menu Social Icons to add icons in your navigation menus. In some cases that may be difficult depending on our site. But just thinking logically and even have some friends test the navigation to make sure it makes sense. Thanks for sharing your worthy thought with us.

    With best regards,

    Amar kumar

    1. Hey Amar,

      The matter of fact is that it is dependent upon the developer of the theme whether to provide one navigation menu or more. WordPress has an amazing feature to handle the menu items.

      You can even add the sub-menu items or we say it the drop down menu. It’s important to show the menu bar and the items to guide your readers about the content.

      Thanks for stopping by.

      Have a great day.

      ~Ravi

Leave a Reply

Your email address will not be published. Required fields are marked *